﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public static class Utility
    {
        #region 公共方法

        /// <summary>
        /// 生成密码。
        /// </summary>
        /// <param name="plainText">密码明文。</param>
        /// <returns>加密后的密码。</returns>
        public string BuildPassword(string plainText)
        {
            if (String.IsNullOrEmpty(plainText))
            {
                return plainText;
            }

            string salt = this.PasswordSalt;

            return BuildPassword(plainText, ref salt);
        }

        /// <summary>
        /// 生成密码。
        /// </summary>
        /// <param name="plainText">密码明文。</param>
        /// <param name="salt"></param>
        /// <returns></returns>
        public static string BuildPassword(string plainText, ref string salt)
        {
            const string passwordFormat = "SHA1";

            if (String.IsNullOrEmpty(plainText))
            {
                return plainText;
            }

            if (String.IsNullOrEmpty(salt))
            {
                salt = Guid.NewGuid().ToString("N");
            }

            string password = FormsAuthentication.HashPasswordForStoringInConfigFile((salt + plainText), passwordFormat);

            return password;
        }

        #endregion
    }
}
